package tree;

public class Test {
    public static void main(String[] args) {
        TestBinaryTree tree = new TestBinaryTree();
        TreeNode root = tree.createTree1();
        Test test = new Test();
        System.out.println(test.isBalanced(root));
    }
    public boolean isBalanced(TreeNode root) {
        if(root == null){
            return true;
        }
        if(Math.abs(calculateDepth(root.left) - (calculateDepth(root.right))) > 1){
            return false;
        }
        boolean leftTree = isBalanced(root.left);
        boolean rightTree = isBalanced(root.right);
        return leftTree && rightTree;
    }
    public int calculateDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        int leftHeight = calculateDepth(root.left);
        int rightHeight = calculateDepth(root.right);
        return Math.max(leftHeight,rightHeight) + 1;
    }
}
